package com.itheima.demo05;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/13 19:03
 */
public class Cooker implements Runnable {
    private Desk desk = new Desk();

    public Cooker(Desk desk) {
        this.desk = desk;
    }

    //吃货类;
    @Override
    public void run() {
        while (true) {
            synchronized (desk.getLock()) {
                if(desk.getCount()==0){
                    break;
                }
                //线判断有没有包子;
                if (desk.isFlag()) {
                    System.out.println("吃货吃了一个包子!");
                    //变更flag状态;
                    desk.setFlag(false);
                    //唤醒等待的线程;
                    desk.getLock().notifyAll();
                    //吃一个就减一个包子;
                    desk.setCount(desk.getCount() - 1);
                } else {
                    try {
                        desk.getLock().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
